package com.example.myapplication.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.tools.BackBanner;
import com.example.myapplication.tools.TimeUtils;
import com.example.myapplication.tools.ToastUtils;
import com.example.myapplication.ui.face.FaceMainActivity;

public class Logo extends AppCompatActivity {

    Context context;
    private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE ,Manifest.permission.INTERNET};
    BackBanner backMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logo);
        context= this;

        // 版本判断。当手机系统大于 23 时，才有必要去判断权限是否获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            // 检查该权限是否已经获取
//            int i = ContextCompat.checkSelfPermission(this, permissions[0]);
//            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
//            if (i != PackageManager.PERMISSION_GRANTED) {
//                // 如果没有授予该权限，就去提示用户请求
//                showDialogTipUserRequestPermission();
//            }
            for(String s : permissions){
                int i = ContextCompat.checkSelfPermission(this, s);
                // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
                if (i != PackageManager.PERMISSION_GRANTED) {
                    // 如果没有授予该权限，就去提示用户请求
                    showDialogTipUserRequestPermission(s);
                }
            }
        }
        if(Build.VERSION.SDK_INT >= 30){
//            int i = ContextCompat.checkSelfPermission(this, Manifest.permission.MANAGE_EXTERNAL_STORAGE);
            boolean isHasStoragePermission= Environment.isExternalStorageManager();
            if (!isHasStoragePermission) {
                // 如果没有授予该权限，就去提示用户请求
                Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                startActivity(intent);
            }

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void onClickVoid(View v){
        int id = v.getId();
        Intent it = new Intent();
        if(id == R.id.button1){
            it.setClass(this,MainActivity.class);
            startActivity(it);
        }else if(id == R.id.button2){
            it.setClass(v.getContext(), NfcExample.class);
            startActivity(it);
        }else if(id == R.id.button3){
            it.setClass(context, MyGridViewTest.class);
            startActivity(it);
        }else if(id == R.id.button4){
            it.setClass(context, PhotoChoose.class);
            startActivity(it);
        }else if(id == R.id.button6){
            it.setClass(context, Door.class);
            startActivity(it);
        }else if(id == R.id.buttonv){
            it.setClass(context, NfcIC.class);
            startActivity(it);
        }else if(id == R.id.button7){
            it.setClass(context, SocketDoor.class);
            startActivity(it);
        }else if(id == R.id.button8){
            it.setClass(context, FingerActivity.class);
            startActivity(it);
        }else if(id == R.id.button9){
            it.setClass(context, IdcardIC.class);
            startActivity(it);
        }else if(id == R.id.button10){
            it.setClass(context, FaceMainActivity.class);
            startActivity(it);
        }else if(id == R.id.button11){
            it.setClass(context, Sip2Activity.class);
            startActivity(it);
        }else if(id == R.id.button12){
            it.setClass(context, ExcelChangeActivity.class);
            startActivity(it);
        }else{

        }
    }

    // 提示用户该请求权限的弹出框
    private void showDialogTipUserRequestPermission(String temp) {

//        new AlertDialog.Builder(this)
//                .setTitle("存储权限不可用")
//                .setMessage(temp + "\n权限为获取，请开启权限")
//                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        startRequestPermission(temp);
//                    }
//                })
//                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        finish();
//                    }
//                }).setCancelable(false).show();
        startRequestPermission(temp);
    }

    // 开始提交请求权限
    private void startRequestPermission(String temp) {
        String[] array = new String[]{temp};
        ActivityCompat.requestPermissions(this, array, 123);
    }



    private AlertDialog dialog;
    // 提示用户去应用设置界面手动开启权限
    private void showDialogTipUserGoToAppSettting() {

        dialog = new AlertDialog.Builder(this)
                .setTitle("存储权限不可用")
                .setMessage("请在-应用设置-权限-中，允许使用存储权限")
                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 跳转到应用设置界面
                        goToAppSetting();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setCancelable(false).show();
    }

    // 跳转到当前应用的设置界面
    private void goToAppSetting() {
        Intent intent = new Intent();

        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);

        startActivityForResult(intent, 123);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123) {

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                // 检查该权限是否已经获取
                for(String s : permissions){
                    int i = ContextCompat.checkSelfPermission(this, s);
                    // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
                    if (i != PackageManager.PERMISSION_GRANTED) {
                        // 如果没有授予该权限，就去提示用户请求
                        showDialogTipUserRequestPermission(s);
                    }
                }

//                int i = ContextCompat.checkSelfPermission(this, permissions[0]);
//                // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
//                if (i != PackageManager.PERMISSION_GRANTED) {
//                    // 提示用户应该去应用设置界面手动开启权限
//                    showDialogTipUserGoToAppSettting();
//                } else {
//                    if (dialog != null && dialog.isShowing()) {
//                        dialog.dismiss();
//                    }
//                    ToastUtils.showMessages("权限获取成功",context);
//                }
            }
        }
    }


}